package com.housediyadmin.view.components
{
	
	/**
	 * 
	 * @author yanghanbing
	 * 2010/11/16
	 */	
	import com.housediyadmin.events.goodsEvents.GetGoodsByTypeIDEvent;
	import com.housediyadmin.events.treeEvents.GetGoodsTreeEvent;
	import com.housediyadmin.model.GoodsModel;
	import com.housediyadmin.model.GoodsTreeModel;
	
	import mx.binding.utils.BindingUtils;
	import mx.controls.Alert;
	import mx.events.ListEvent;
	
	import org.robotlegs.mvcs.Mediator;
	
	public class GoodsMediator extends Mediator
	{
		[Inject]
		public var view:GoodsView;
		
		[Inject]
		public var goodsTypeModel:GoodsTreeModel;
		
		[Inject]
		public var goodsModel:GoodsModel;
		
		//public var selectedNode:XML;
		public var typeIdArray:Array;
		
		override public function onRegister():void
		{
			BindingUtils.bindProperty(view.goodsTypeTree, "dataProvider", goodsTypeModel, "goodsTreeXML");
			BindingUtils.bindProperty(view.goodsList, "dataProvider", goodsModel, "goodsArray");		
			dispatch(new GetGoodsTreeEvent(GetGoodsTreeEvent.GET_GOODS_TREE));
			
			view.goodsTypeTree.addEventListener(ListEvent.CHANGE, goodsTypeClickHandler);
			view.goodsTypeTree.addEventListener(ListEvent.ITEM_DOUBLE_CLICK,changeTypeNameHandler);
			view.goodsTypeTree.addEventListener(ListEvent.ITEM_EDIT_BEGINNING,editTypeBeginHandler);			
		}
		
		public function goodsTypeClickHandler(event:ListEvent):void
		{
			goodsTypeModel.selectedNode = (event.target).selectedItem as XML;
			typeIdArray = new Array();
			if(goodsTypeModel.selectedNode.children().length() == 0 && goodsTypeModel.selectedNode.@typeLevel != 3 )
			{
				Alert.show("暂无商品");
				goodsModel = new GoodsModel();
			}
			else if(goodsTypeModel.selectedNode.@typeLevel == 3)
			{
				var l:int = (int)(goodsTypeModel.selectedNode.@typeID.toString());
				typeIdArray.push(l);
				dispatch(new GetGoodsByTypeIDEvent(GetGoodsByTypeIDEvent.GET_GOODS_BY_TYPEID, typeIdArray, null));					
			}
			else
			{
				var xml:XMLList = goodsTypeModel.selectedNode..type3;
				for(var i:int = 0; i<xml.length(); i++)
				{	
					var k:int = (int)(xml[i].@typeID.toString()) ;				 
					typeIdArray.push(k);
				}
				dispatch(new GetGoodsByTypeIDEvent(GetGoodsByTypeIDEvent.GET_GOODS_BY_TYPEID, typeIdArray, null));
			}
		}
		
		public function changeTypeNameHandler(event:ListEvent):void
		{			
			view.goodsTypeTree.editable = true;
			view.goodsTypeTree.addEventListener(ListEvent.ITEM_EDIT_END,editTypeEndHandler);
			view.goodsTypeTree.editedItemPosition = {columnIndex:0, rowIndex:event.rowIndex};
		}
		
		protected function editTypeBeginHandler(event:ListEvent):void
		{
			event.preventDefault();
		}
		
		protected function editTypeEndHandler(event:ListEvent):void
		{
			event.preventDefault();
			view.goodsTypeTree.removeEventListener(ListEvent.ITEM_EDIT_END, editTypeEndHandler);
			var typeName:String = event.currentTarget.itemEditorInstance.text;			
			goodsTypeModel.selectedNode.@typeName = typeName;
			view.goodsTypeTree.destroyItemEditor();
		}
	}
	
}